//
//  AppData.m
//  drinkApp
//
//  Created by HexPang on 14/8/29.
//  Copyright (c) 2014年 CnbizMedia. All rights reserved.
//

#import "AppData.h"


@implementation AppData

+ (instancetype)sharedClient {
    static AppData *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[AppData alloc] init];
    });
    return _sharedClient;
}

- (BOOL)isLogin{
    NSString *token = [[NSUserDefaults standardUserDefaults] valueForKey:@"token"];
    if([token isEqualToString:@""] || token == nil){
        return NO;
    }
    return YES;
}

- (void)initData{
    NSDictionary *district = [[NSUserDefaults standardUserDefaults] valueForKey:DISTRICT_KEY];
    if(district == nil){
        [[AppAPIClient sharedClient] GET:@"AppData/District" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *result = responseObject[@"data"];
            [[NSUserDefaults standardUserDefaults] setObject:result forKey:DISTRICT_KEY];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Cannot load data:%@",operation.responseString);
        }];
    }
}

- (NSDictionary *)allDistrict{
    return [[NSUserDefaults standardUserDefaults] valueForKey:DISTRICT_KEY];
}

- (NSString *)districtNameBy:(NSString *)disId{
    if([disId isEqualToString:@"0"]){
        return @"";
    }
    NSDictionary *district = [[NSUserDefaults standardUserDefaults] valueForKey:DISTRICT_KEY];
    if(district == nil){
        return nil;
    }
    NSDictionary *dis = [district objectForKey:disId];
    if(dis == nil) return nil;
    return dis[@"name"];
    
}
//failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
//cancel:(void (^)())cancel
- (void)showShareDialog:(NSString*)url withTitle:(NSString*)title description:(NSString*)description forView:(UIView*)view withImage:(UIImage*)image cancel:(void (^)())cancel failure:(void (^)(int errorCode, NSString *errorMessage))failure result:(void (^)(NSDictionary *respones))result{
//    [BDSocialShareContent shareContentWithDescription:description url:url title:description];
//    BDSocialShareContent*bds=[[BDSocialShareContent alloc]init];
//    [bds addImageWithThumbImage:image imageSource:image];

    
//    FrontiaShareContent *content = [[FrontiaShareContent alloc] init];
//    content.url = url;
//    content.description = description;
    //content.title = @"Frontia组件”;
//    content.imageObj = image;
//    content.title = content.description;
    
    //content.imageUrl = @"http://apps.bdimg.com/developer/static/04171450/developer/images/icon/terminal_adapter.png";
    
//    NSArray *sharePlatforms =
//    @[kBD_SOCIAL_SHARE_PLATFORM_WEIXIN_SESSION,
//      kBD_SOCIAL_SHARE_PLATFORM_WEIXIN_TIMELINE,
//      kBD_SOCIAL_SHARE_PLATFORM_SINAWEIBO,
//      kBD_SOCIAL_SHARE_PLATFORM_QQWEIBO,
//      kBD_SOCIAL_SHARE_PLATFORM_QQFRIEND,
//      kBD_SOCIAL_SHARE_PLATFORM_EMAIL,
//      kBD_SOCIAL_SHARE_PLATFORM_SMS,
//      kBD_SOCIAL_SHARE_PLATFORM_COPY];

    
//    FrontiaShare *share = [Frontia getShare];
//    [share registerSinaweiboAppId:KEY_SINA_WEIBO];
//    [share registerQQAppId:QQ_APP_KEY enableSSO:YES];
//    [share registerWeixinAppId:WEIXIN_APP_KEY];
//
//    [share showShareMenuWithShareContent:content displayPlatforms:sharePlatforms supportedInterfaceOrientations:UIInterfaceOrientationMaskAllButUpsideDown isStatusBarHidden:NO targetViewForPad:view cancelListener:^{
//        cancel();
//    } failureListener:^(int errorCode, NSString *errorMessage) {
//        failure(errorCode,errorMessage);
//    } resultListener:^(NSDictionary *respones) {
//        result(respones);
//    }];
}
+ (NSString*)deviceType{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    
    NSString *device = [NSString stringWithUTF8String:machine];
    free(machine);
    return device;
}
+ (NSString *) platformString{
    NSString *platform = [self deviceType];
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4 (GSM)";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4 (CDMA)";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5C";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5C";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5S";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5S";
    if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
    
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (CDMA)";
    
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (GSM)";
    if ([platform isEqualToString:@"iPad4,3"])      return @"iPad Air (CDMA)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini Retina (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad Mini Retina (CDMA)";
    
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}
@end
